<template>
  <el-dialog
    :title="'发送'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
    <el-form-item v-if="dataForm.sendType === 1" label="下发会员" prop="userIds">
      <el-select  filterable multiple v-model="dataForm.userIds" placeholder="请选择">
        <el-option
          v-for="item in users"
          :key="item.id"
          :label="item.username"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item v-if="dataForm.sendType === 3" label="下发商品" prop="goodsId">
      <el-select filterable multiple v-model="dataForm.goodsId" placeholder="请选择">
        <el-option
          v-for="item in goods"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item v-if="dataForm.sendType === 1" label="发送短信" prop="sendSms">
      <el-checkbox v-model="dataForm.sendSms " >发送短信</el-checkbox>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          sendType: '',
          goodsIds: [],
          userIds: [],
          sendSms: ''
        },
        goods: [],
        users: [],
        dataRule: {
          name: [
            { required: true, message: '优惠卷名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, sendType) {
        this.dataForm.id = id
        this.dataForm.sendType = sendType
        this.visible = true
        this.getGoods()
        this.getUsers()
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/platform/coupon/publish`),
              method: 'post',
              data: this.$http.adornData({
                sendType: this.dataForm.sendType,
                couponId: this.dataForm.id,
                goodsIds: this.dataForm.goodsId,
                userIds: this.dataForm.userIds,
                sendSms: this.dataForm.sendSms
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      getGoods () {
        this.goods = []
        this.$http({
          url: this.$http.adornUrl('/platform/goods/queryAll'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.goods = data.data
        })
      },
      getUsers () {
        this.users = []
        this.$http({
          url: this.$http.adornUrl('/platform/user/queryAll'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.users = data.data
        })
      }
    }
  }
</script>
